package com.xiaoma.tpo.service;

import com.xiaoma.tpo.dao.UserKeyDao;
import com.xiaoma.tpo.dao.UserWinNumDao;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.domain.UserKey;
import com.xiaoma.tpo.domain.UserWinNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by Owner on 2015/5/12.
 */
@Service
public class UserKeyService {
    @Autowired
    private UserKeyDao userKeyDao;

    @Autowired
    private UserWinNumDao userWinNumDao;


    /**
     * 记录用户获奖次数
     * @param user 用户对象
     * @param num  获奖次数
     * @return 1成功 11达到最大次数
     */
    public int addWinNum(User user , int activityId){
        UserWinNum wun = new UserWinNum();
        int count = userWinNumDao.getWinNUmByUser(user.getId());
        if(count<3){
            wun.setUserId(user.getId());
            wun.setActivityId(activityId);
            wun.setCreateDate(new Date());
            userWinNumDao.insert(wun);
            return 1;
        }else{
            return 11;
        }

    }

    /**
     * 生成我的key
     * @param user
     * @return
     */
    public UserKey createKey(User user){
        UserKey uk = new UserKey();
        uk.setUserId(user.getId());
        String key = genRandomNum(6);
        uk.setKeyCode(key);
        List<UserKey> list = userKeyDao.findByColumns(uk);
        while(list.size()>0){
            key = genRandomNum(6);
            uk.setKeyCode(key);
            list = userKeyDao.findByColumns(uk);
        }
        //获得用户获奖次数
        uk.setCreateDate(new Date());
        uk.setStatus(0);
        userKeyDao.insert(uk);
        return uk;
    }


    /**
     * 获取我的key
     * @param user 用户对象
     * @param currentPage  当前页
     * @param pageSize  每页数量
     * @return
     */
    public List<UserKey> getMyKeys(User user,int currentPage,int pageSize){
        List<UserKey> list = userKeyDao.getMyKeys(user.getId(), currentPage, pageSize);
        return list;
    }

    /**
     * 使用key
     * @param user
     * @param key
     * @return  0=成功   1=已被使用  2=key未找到
     */
    public int useKey(User user ,String key){
        int status = 0 ;
        UserKey uk = new UserKey();
        uk.setUserId(user.getId());
        uk.setKeyCode(key);
        List<UserKey> list = userKeyDao.findByColumns(uk);
        if(list.size()>0){
            uk = list.get(0);
            if(uk.getStatus()==0){
                uk.setStatus(1);
                uk.setUseDate(new Date());
                userKeyDao.update(uk);
            }else{
                status=1;
            }
        }else{
            status =2;
        }

        return status;
    }
    
    /**
     * 根据key得到userkey
     * @param key
     * @return
     */
    public UserKey findByKey(String key) {
    	UserKey userKey = new UserKey();
    	userKey.setKeyCode(key);
    	List<UserKey> list = userKeyDao.findByColumns(userKey);
    	if(list.size() > 0) {
    		return list.get(0);
    	}else {
    		return null;
    	}
    }
    
    public List<UserKey> getLasted(int userId) {
        return userKeyDao.getLasted(userId);
    }

    /**
     * 生成随机码
     * @param lenght 生成码长度
     * @return
     */
    public static String genRandomNum(int lenght){
        //35是因为数组是从0开始的，26个字母+10个数字
        final int  maxNum = 36;
        int i;  //生成的随机数
        int count = 0; //生成的密码的长度
        char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
                'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while(count < lenght){
            //生成随机数，取绝对值，防止生成负数，
            i = Math.abs(r.nextInt(maxNum));  //生成的数最大为36-1
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count ++;
            }
        }

        return pwd.toString();
    }
    
}
